<?php
namespace Home\Model;
use Think\Model;
class MemberAddressModel extends Model {
	public $table='zjy_member_address';
	public $page; //当前页
	public $perpage; //每页显示
	public $match_id; //ID
	public $pc_id; //分类ID
	public $post; //提交的信息 数组
	public $ids; //IDs 主要用于删除
	public $sorts; //IDs 主要用于删除
	public $keyword; //关键词
	public $pc_ids; //获取分类ID下的子分类
	public $member_id; //

	//获取用户地址列表
	public function get_member_address_list(){
		$Model=M("Member_address");
		$member_id=intval($this->member_id);
		$list=$Model->where('member_id='.$member_id)->select();
		return $list;
	}
	//获取用户地址总数
	public function get_member_address_total()
	{
		//搜索
		$member_id=intval($this->member_id);
		$where='where 1 '.'and member_id='.$member_id;
		/*if($this->keyword){
			$where.="and `order_sn` LIKE '%".$this->keyword."%'";
		}
		if($this->pc_id){
			$pc_ids=$this->get_sub_pc_id($this->pc_id);
			$pc_ids=mb_strcut($pc_ids,1); //裁掉第一个，
			$where.="and pc_id in(".$pc_ids.")";
		}*/
		$Model=new Model();
		$query=$Model->query("select count(rec_id) as total from ".$this->table." ".$where." ");
		return $query[0]['total'];
	}
	//获取用户地址列表
	public function get_member_address_info(){
		$Model=M("Member_address");
		$rec_id=intval($this->rec_id);
		$list=$Model->where('rec_id='.$rec_id)->find();
		return $list;
	}
	//提交新地址
	public function member_address_add_post(){
		$Model=M('Member_address');
		$address_total=intval($this->address_total);
		if($address_total>=5){
			return false;
		}
		$data=$this->post;
		if($Model->add($data)){
			return true;
		}else{
			return false;
		}
	}
	//新用户默认地址
	public function member_address_post(){
		$Model=M('Member_address');
		$rec_id=$Model->where('member_id='.$this->post['member_id'])->getField('rec_id');
		if($rec_id){
			return true;
		}
		$data=$this->post;
//		$data['default']=1;
		if($Model->add($data)){
			return true;
		}else{
			return false;
		}
	}
	//修改地址提交
	public function member_address_edit_post($id){
		$Model=M("Member_address");
		$condition['rec_id'] = $id;
		$data=$this->post;
		// print_r($this->post);exit;
		$result=$Model->where($condition)->save($data);
		// $sql=$Model->getLastSql();
		// echo $sql;exit;
		if($result === false) {
			return false;
		}else{
			return true;
		}
	}
	//删除
	public function member_address_delete(){
		$ids=$this->ids;
		$Model=M("Member_address");
		foreach($ids as $v){
			$condition['rec_id']=$v;
			if(!$Model->where($condition)->delete()){
				return false;
			}
		}
		return true;
	}
	//默认地址
	public function member_address_default_set(){
		$ids=$this->ids;
		$Model=M("Member_address");
		foreach($ids as $v){
//			$condition['rec_id']=$v;
			if(!$Model->where('rec_id<>'.$v)->setField('default','0')||!$Model->where('rec_id='.$v)->setField('default','1')){
				return false;
			}
		}
		return true;
	}
}